package org.ljy.lotterysystem.controller;

import org.apache.ibatis.annotations.Param;
import org.ljy.lotterysystem.common.errorcode.ControllerErrorCodeConstants;
import org.ljy.lotterysystem.common.exception.ControllerException;
import org.ljy.lotterysystem.common.pojo.CommonResult;
import org.ljy.lotterysystem.common.utils.JacksonUtil;
import org.ljy.lotterysystem.controller.param.CreatePrizeParam;
import org.ljy.lotterysystem.controller.param.PageParam;
import org.ljy.lotterysystem.controller.result.FindPrizeListResult;
import org.ljy.lotterysystem.service.IPictureService;
import org.ljy.lotterysystem.service.IPrizeService;
import org.ljy.lotterysystem.service.dto.PageListDTO;
import org.ljy.lotterysystem.service.dto.PrizeDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.stream.Collectors;

@RestController
public class PrizeController {

    @Autowired
    private IPictureService pictureService;


    private static final Logger logger = LoggerFactory.getLogger(UserController.class);


    @Autowired
    private IPrizeService prizeService;


    /**
     *  上传图片
     * @param file
     * @return
     */
    @RequestMapping("/pic/upload")
    public String upload(MultipartFile file) {
        return pictureService.savePicture(file);
    }


    /**
     *  创建奖品
     * @param param
     * @param prizePic
     * @return
     */
    @RequestMapping("/prize/create")
    public CommonResult<Long> createPrize(@Validated @RequestPart("param") CreatePrizeParam param,
                                          @RequestPart("prizePic") MultipartFile prizePic) {
        //1.
        logger.info("PrizeController createPrize:{}", JacksonUtil.writeValueAsString(param));
        return CommonResult.success(prizeService.createPrize(param,prizePic));
    }


    /**
     * 获取奖品列表
     * @param param
     * @return
     */
    @RequestMapping("/prize/find-list")
    public CommonResult<FindPrizeListResult> findPrizeList(@Param("param") PageParam param) {
        logger.info("PrizeController findPrizeList:{}",JacksonUtil.writeValueAsString(param));
        PageListDTO<PrizeDTO> pageListDTO = prizeService.findPrizeList(param);
        return CommonResult.success(convertToFindPrizeResult(pageListDTO));
    }


    private FindPrizeListResult convertToFindPrizeResult(PageListDTO<PrizeDTO> pageListDTO) {
        if(pageListDTO == null) {
            throw new ControllerException(ControllerErrorCodeConstants.PRIZE_ERROR);
        }
        FindPrizeListResult result = new FindPrizeListResult(); //创建返回结果
        //构造返回结果
        result.setTotal(pageListDTO.getTotal());
        result.setRecords(
                pageListDTO.getData().stream().map(
                        prizeDTO -> {
                            FindPrizeListResult.PrizeInfo prizeInfo = new FindPrizeListResult.PrizeInfo();//实现内部类（data）
                            prizeInfo.setPrizeId(prizeDTO.getId());
                            prizeInfo.setPrizeName(prizeDTO.getName());
                            prizeInfo.setDescription(prizeDTO.getDescription());
                            prizeInfo.setImageUrl(prizeDTO.getImageUrl());
                            prizeInfo.setPrice(prizeDTO.getPrice());
                            return prizeInfo;
                        }).collect(Collectors.toList())
        );
        return result;
    }

}
